package com.bauer.base.thread;

public class ThreadLoaclDemo {

    private static ThreadLocal<Long> str = new ThreadLocal<>();

    private static long str2 = 0l;


    public static void main(String[] args) {

        Runnable runnable = new MyRunnable(str, str2);
        Thread thread1 = new Thread(runnable);
        Thread thread2 = new Thread(runnable);

        thread1.start();
        thread2.start();
    }

    static class MyRunnable implements Runnable {

        private ThreadLocal<Long> str;

        private Long str2;


        public MyRunnable(ThreadLocal<Long> str, Long str2) {
            this.str = str;
            this.str2 = str2;
        }

        @Override
        public void run() {
            for (long i = 1000000; i < 1000200; i++) {
                str.set(i);
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            for (long i = 1000000; i < 1000200; i++) {
                str2 = i;
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            System.out.println("str:" + str.get() + Thread.currentThread().getName());
            System.out.println("str2:" + str2 + Thread.currentThread().getName());
        }
    }
}
